﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NKD07.Admin
{
    public partial class add_product : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCategory();
                InitProductInfo();
            }
        }

        // load and show all name of categories in database
        private void LoadCategory()
        {
            Classes.category category = new Classes.category();
            DataTable tb = category.getAllCategories();
            dlCaregories.DataSource = tb;
            dlCaregories.DataBind();
        }

        // load all information of product to edit
        private void InitProductInfo()
        {
            DataTable tb = new DataTable();

            // insert new row
            DataColumn column = new DataColumn();
            column.DataType = System.Type.GetType("System.Int32");
            column.ColumnName = "init";
            tb.Columns.Add(column);
            // insert new row
            DataRow row = tb.NewRow();
            row["init"]= 0;
            tb.Rows.Add(row);

            // bind data into control : dlProductInfo
            dlProductInfo.DataSource = tb;
            dlProductInfo.DataBind();
            LoadCategoryList();
            LoadManufacturerList();

            // initialize value for calendar control
            AjaxControlToolkit.CalendarExtender calendar = (AjaxControlToolkit.CalendarExtender)dlProductInfo.FindControl("cldDateAdded");
            calendar.SelectedDate = DateTime.Today;

        }

        // load all categories to edit infor of product
        private void LoadCategoryList()
        {
            Classes.category category = new Classes.category();
            DataTable tb = category.getAllCategories();
            DropDownList ddlCategory = (DropDownList)dlProductInfo.FindControl("ddlCategory");
            for (int index = 0; index < tb.Rows.Count; index++)
            {
                ddlCategory.Items.Add(new ListItem(tb.Rows[index]["name"].ToString(), tb.Rows[index]["id"].ToString()));
            }
        }

        // load all manufacturer to initialize infor of product
        private void LoadManufacturerList()
        {
            Classes.manufacturer manufacturer = new Classes.manufacturer();
            DataTable tb = manufacturer.getAllManufacturer();
            DropDownList ddlPublisher = (DropDownList)dlProductInfo.FindControl("ddlPublisher");
            for (int index = 0; index < tb.Rows.Count; index++)
            {
                ddlPublisher.Items.Add(new ListItem(tb.Rows[index]["name"].ToString(), tb.Rows[index]["id"].ToString()));
            }
        }

        // insert new product into database
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            TextBox txtName = (TextBox)dlProductInfo.FindControl("txtName");
            string productName = txtName.Text.Trim();

            TextBox txtDescription = (TextBox)dlProductInfo.FindControl("txtDescription");
            string productDescription = txtDescription.Text.Trim();

            TextBox txtStock = (TextBox)dlProductInfo.FindControl("txtStock");
            string productStock = txtStock.Text.Trim();

            TextBox txtNumberLike = (TextBox)dlProductInfo.FindControl("txtNumberLike");
            string productNumberLike = txtNumberLike.Text.Trim();

            TextBox txtNumberDislike = (TextBox)dlProductInfo.FindControl("txtNumberDislike");
            string productNumberDislike = txtNumberDislike.Text.Trim();

            TextBox txtPriceSell = (TextBox)dlProductInfo.FindControl("txtPriceSell");
            string productPriceSell = txtPriceSell.Text.Trim();

            TextBox txtMapping = (TextBox)dlProductInfo.FindControl("txtMapping");
            string productMapping = txtMapping.Text.Trim();

            DropDownList ddlCategory = (DropDownList)dlProductInfo.FindControl("ddlCategory");
            string productCategory = ddlCategory.SelectedValue;

            DropDownList ddlPublisher = (DropDownList)dlProductInfo.FindControl("ddlPublisher");
            string productPublisher = ddlPublisher.SelectedValue;

            AjaxControlToolkit.CalendarExtender clDateAdded = (AjaxControlToolkit.CalendarExtender)dlProductInfo.FindControl("cldDateAdded");
            DateTime productDateAdded = clDateAdded.SelectedDate.Value;

            string productImage = "";
            FileUpload fileUpload = (FileUpload)dlProductInfo.FindControl("fuProductImage");
            if (fileUpload.HasFile)
            {
                try
                {
                    fileUpload.SaveAs(Server.MapPath("~/images/Products/") + fileUpload.FileName);
                    productImage = "./images/Products/" + fileUpload.FileName;
                }
                catch (Exception)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alertInsert", "UploadResult('There is problem uploading your image! upload failed...');", true);
                    return;
                }
            }

            Classes.product product = new Classes.product();
            product.Name = productName;
            product.Description = productDescription;
            product.NumberLike = int.Parse(productNumberLike);
            product.NumberDislike = int.Parse(productNumberDislike);
            product.PriceSell = float.Parse(productPriceSell);
            product.IdCategory = int.Parse(productCategory);
            product.IdManufacturer = int.Parse(productPublisher);
            product.Mapping = productMapping;
            product.Stock = int.Parse(productStock);
            product.ImageUrl = productImage;
            product.DateAdded = productDateAdded;

            bool result = product.InsertProduct(product);
            if (result == false)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alertInsert", "UploadResult('Insert failed! Please try again... ');", true);
                return;
            }
            else
            {

            }
        }

    }
}